package javabase.thinkinjava.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author fance
 * @date 18-4-30 上午7:59
 */
public class GetChannel {
    public static final int BSIZE = 1024;

    public static void main(String[] args) throws IOException {
        FileChannel fileChannel = new FileOutputStream("data.txt").getChannel();
        fileChannel.write(ByteBuffer.wrap("some txt".getBytes()));
        fileChannel.close();

       fileChannel = new RandomAccessFile("data.txt","rw").getChannel();
       fileChannel.position(fileChannel.size());

       fileChannel.write(ByteBuffer.wrap("some more".getBytes()));
       fileChannel.close();

       fileChannel = new FileInputStream("data.txt").getChannel();
       ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
        fileChannel.read(buffer);
        buffer.flip();
        while (buffer.hasRemaining()) {
            System.out.print((char)buffer.get());
        }
    }
}
